﻿using HPIT_LiuzuPoint.Common;
using HPIT_LiuzuPoint.Entity.DTOModel;
using HPIT_LiuzuPoint.Entity.POCOModel;
using HPIT_LiuzuPoint.Entity.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HPIT_LiuzuPoint.Business
{
    public class UserlistService : BaseService<UserInfo>
    {
        /// <summary>
        /// 对前台页面进行分页
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult UserList(SearchViewModel viewModel)
        {
            int rowCount = 0;
            //利用lambda表达式返回一个IQueryable的接口
            var query = PredicateExtensions.True<UserInfo>();

            if (!string.IsNullOrWhiteSpace(viewModel.Name))
                query = query.And(m => m.Name == viewModel.Name);
            if (viewModel.Roomsize > 0)
                query = query.And(m => m.RoomId == viewModel.Roomsize);

            //调用BaseService封装好的分页方法
            var list = GetPagedList(viewModel.page, viewModel.rows, ref rowCount, query, m => m.Id, false)
                .Select(a => new UserViewModel() {

                    Id = a.Id,
                    Name = a.Name,
                    Roomprice = a.Roomprice,
                    Drinks = a.Drinks,
                    Consume = a.Consume,
                    RoomName = a.Room.RoomName

                });

            var data = new EasyUIDataGrid() { total = rowCount, rows = list };

            return new OperateResult(ResultSatae.Success, "", data);            

            
        }
        /// <summary>
        /// 添加前台的业务
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult AddUserlist(ReceptionEditViewModel viewModel)
        {
            //实例化出poco模型
            var model = new UserInfo()
            {
                Id = viewModel.Id,
                Name = viewModel.Name,
                Roomprice = viewModel.Roomprice,
                Drinks = viewModel.Drinks,
                Consume = viewModel.Consume,
                RoomId = viewModel.RoomId
            };
            //调用父类中公共的添加方法
            if (Add(model))
            {
                return new OperateResult(ResultSatae.Success, "添加成功");
            }
            else
            {
                return new OperateResult(ResultSatae.Success, "添加失败");
            }
        }
        /// <summary>
        /// 根据id查询前台实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperateResult GetUserlist(int id)
        {
            var model = Find(e => e.Id == id);
            if (model != null) 
            {
                var viewModel = new ReceptionEditViewModel()
                {
                    Id = model.Id,
                    Name = model.Name,
                    Roomprice = model.Roomprice,
                    Drinks = model.Drinks,
                    Consume = model.Consume,
                    RoomId = model.RoomId
                };

                return new OperateResult(ResultSatae.Success,"", viewModel);
            }
            else
            {
                return new OperateResult(ResultSatae.Error, "该学员未找到");
            }
        }
        /// <summary>
        /// 更新前台信息的业务
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult Edituserlist(ReceptionEditViewModel viewModel)
        {
            //调用父类里面封装的公共查询方法
            var model = Find(e => e.Id == viewModel.Id);
            if (model != null)
            {
                model.Id = viewModel.Id;
                model.Name = viewModel.Name;
                model.Drinks = viewModel.Drinks;
                model.Consume = viewModel.Consume;
                model.Roomprice = viewModel.Roomprice;
                model.RoomId = viewModel.RoomId;

                if (Update(model))
                {
                    return new OperateResult(ResultSatae.Success, "更新成功");
                }
                else
                {
                    return new OperateResult(ResultSatae.Error, "网络繁忙，请稍后重试");
                }
            }
            else
            {
                return new OperateResult(ResultSatae.Error, "该学员不存在");
            }

           
        }
        public OperateResult Deleteuserlist(int id)
        {
            var model = Find(e => e.Id == id);
            if (model != null)
            {
                if (Delete(model))
                {
                    return new OperateResult(ResultSatae.Success, "删除成功");
                }
                else
                {
                    return new OperateResult(ResultSatae.Error, "网络繁忙，请稍后重试");
                }

            }
            else
            {
                return new OperateResult(ResultSatae.Error, "该学员已删除，请刷新后重试");
            }
        }
    }
}
